import axios from 'axios'

// 设置基准路径
var baseUrl = 'http://localhost:8888/api/private/v1'
axios.defaults.baseURL = baseUrl

// 拦截器
axios.interceptors.request.use(function (config) {
  // Do something before request is sent
  var token = sessionStorage.getItem('mytoken')
  config.headers.Authorization = token
  return config
}, function (error) {
  // Do something with request error
  return Promise.reject(error)
})

// 登录
export const postLogin = (params) => {
  return axios.post('login', params).then(res => {
    return res.data
  })
}

// 左侧菜单
export const getLeftMenus = () => {
  return axios.get('menus').then(res => {
    return res.data
  })
}

// 获取用户列表数据
export const getUsersList = (params) => {
  return axios.get('users', {params: params}).then(res => {
    return res.data
  })
}

// 添加用户
export const addUser = (params) => {
  return axios.post('users', params).then(res => {
    return res.data
  })
}

// 修改用户状态
export const putUser = (params) => {
  return axios.put(`users/${params.uId}/state/${params.type}`).then(res => {
    return res.data
  })
}

// 编辑用户提交
export const editUser = (params) => {
  return axios.put(`users/${params.id}`, params).then(res => {
    return res.data
  })
}

// 删除单个用户
export const delUser = (id) => {
  return axios.delete(`users/${id}`).then(res => {
    return res.data
  })
}

// 授权角色
export const roleUser = (params) => {
  return axios.put(`users/${params.id}/role`, {rid: params.rid}).then(res => {
    return res.data
  })
}

// 获取权限列表
export const getRightList = (type) => {
  return axios.get(`rights/${type}`).then(res => {
    return res.data
  })
}

// 获取角色列表
export const getRoleList = () => {
  return axios.get('roles').then(res => {
    return res.data
  })
}

// 添加角色
export const addRoleList = (params) => {
  return axios.post('roles', params).then(res => {
    return res.data
  })
}

// 授权角色
export const empowerRole = (params) => {
  return axios.post(`roles/${params.roleId}/rights`, {rids: params.rids}).then(res => {
    return res.data
  })
}

// 删除角色指定权限
export const delRolePower = (params) => {
  return axios.delete(`roles/${params.roleId}/rights/${params.rightId}`).then(res => {
    return res.data
  })
}

// 获取商品列表数据
export const getCategories = (params) => {
  return axios.get(`categories`, {params: params}).then(res => {
    return res.data
  })
}

// 添加分类
export const addCategories = (params) => {
  return axios.post(`categories`, params).then(res => {
    return res.data
  })
}

// 商品列表数据
export const getGoodsList = (params) => {
  return axios.get(`goods`, {params: params}).then(res => {
    return res.data
  })
}

// 添加商品
export const addGoodsList = (params) => {
  return axios.post(`goods`, params).then(res => {
    return res.data
  })
}
